#ifndef _ACCOUNTMANAGER_H
#define _ACCOUNTMANAGER_H

#include "profile/profiledata.h"
#include <QObject>
#include <QVariant>
#include <QTimer>
#include <QMutex>
#include "data/userprofile.h"

namespace Network {
class HttpRequest;
}

class LoginRequest;
class LoginResponse;

class MakeAccountRequest;
class MakeAccountResponse;

class GetFriendRequest;

class OnlineStatusEvent;

class RecentContactRequest;
class RecentContactEvent;


namespace Account {
class Member;
class User;
class Buddy;
}

using namespace Account;
class AccountManager : public QObject
{

    Q_OBJECT

public:
    explicit AccountManager(QObject *parent = 0);

    static AccountManager *instance(QObject *parent = 0);

    void init(const LoginResponse& loginResponse);

    void init(const MakeAccountResponse& makeAccountResponse);

    void updateOnlineStatus(const OnlineStatusEvent &statusEvent);
    void updateRecentContactTime(const RecentContactEvent &recentContactEvent);
    void clearData();
    void postRequestGetProfile();

private:

    static AccountManager *m_instance;
    int m_getFriendRequestPage;

    bool m_isOnlineStatusInitialized; 

    RecentContactRequest *m_recentContactRequest;


    void setConnection() const;

    bool postRecentContactRequest();

    void initOnlineStatus();
    void doProcessLoginSuccess();

signals:
    void logined(bool successed, const QString &message, const QString &sessionKey);
    void activedCode(int successed);
    void aboutToBeLogouted();
    void logouted();
    void dataCleared();
    void requireLogin();
    void notifyError(int type);   //None 0, Network 1,Syntax 2, Property 3

public slots:
    void onMakeAccountResponsed(Network::HttpRequest* httpRequest);
    void onLoginResponsed(Network::HttpRequest* httpRequest);
    void onGetActiveCodeResponsed(Network::HttpRequest* httpRequest);

    void onSessionKeyReady();
    void onWebSocketConnected();
    void onGetProfileResponse(Network::HttpRequest* httpRequest);
public:
    bool getActiveCodeRequest(QString phone);
    bool login(QString userName, QString pass);
    bool createNewAccount(QString phone, QString code);
    void aboutToLogout();
    void logout();

    qint64 getUserId()const {
        return _currentUserId;
    }


    QString getSessionKey() ;

    inline UserProfile* getUserProfile()const{
        return _userProfile;
    }

    inline void setUserProfile(const int userId, const QString& userName, const QString& displayName, const QString& avatar){
        if(_userProfile == NULL){
            _userProfile = new UserProfile(userId, userName, displayName, avatar);
        }else{
            _userProfile->setBaseInfo(userId, userName, displayName, avatar);
        }
    }
private:

    qint64 _currentUserId;
    QString _session_key;
    UserProfile* _userProfile;

};

 // namespace Account

#endif // ACCOUNT_ACCOUNTMANAGER_H
